import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_login/se_login_logic.dart';
import 'package:squat_easy/se_route.dart';
import 'package:squat_easy/se_standard/se_app_colors.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeDialogs {
  /// 登录弹窗
  static void showLoginDialog() {
    login(
      description: 'Login expired, please log in again',
      confirmTitle: 'Login',
      keepSingle: true,
      onConfirm: () {
        SeLogoutLogic.logout();
        SeRoute.openLogin();
      },
    );
  }

  static void showPrivacy({VoidCallback? onConfirm}) {
    SmartDialog.show(
      builder: (context) {
        List<Widget> children = [];

        ///图标
        children.add(
          Image.asset(
            Assets.pngLogo,
            width: 70.w,
          ).paddingTop(26.w),
        );

        /// 标题
        children.addAll([
          const TextWidget.moreLine(text: 'SquatEasy', fontSize: 20, color: Colors.black).paddingTop(15.w),
        ]);

        /// 描述
        children.add(Text.rich(
          TextSpan(
            text: 'By using our App you agree with our \n',
            children: [
              TextSpan(
                text: 'Term of Use',
                style: const TextStyle(color: SeAppColors.primary),
                recognizer: TapGestureRecognizer()..onTap = SeRoute.openProtocolPage,
              ),
              const TextSpan(text: ' and '),
              TextSpan(
                text: 'Privacy Policy.',
                style: const TextStyle(color: SeAppColors.primary),
                recognizer: TapGestureRecognizer()..onTap = SeRoute.openPrivacyPage,
              ),
            ],
          ),
          style: const TextStyle(color: Colors.black, fontSize: 14, height: 1.5),
          textAlign: TextAlign.center,
        ).paddingTop(15.w));

        return Container(
          width: ScreenUtil().screenWidth - 30.w * 2,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(18.w)),
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ...children,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  ButtonWidget.text(
                    'Cancel',
                    textColor: Colors.black,
                    borderRadius: 42.w,
                    borderColor: Colors.black,
                    borderWidth: 1.w,
                    width: 100.w,
                    height: 42.w,
                    fontSize: 14,
                    alignment: Alignment.center,
                    onTap: () async => await SmartDialog.dismiss(),
                  ),
                  ButtonWidget.text(
                    'Agree and Continue',
                    textColor: Colors.black,
                    borderRadius: 42.w,
                    width: 170.w,
                    height: 42.w,
                    fontSize: 14,
                    alignment: Alignment.center,
                    gradient: SeAppColors.primaryGradient,
                    onTap: () async {
                      await SmartDialog.dismiss();
                      onConfirm?.call();
                    },
                  ),
                ],
              ).padding(top: 20.w, bottom: 25.w),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: 'privacy',
    );
  }

  static void oneButton({
    required String title,
    String? confirmTitle,
    VoidCallback? onConfirm,
    String? tag,
    VoidCallback? onDismiss,
    bool keepSingle = false,
  }) {
    SmartDialog.show(
      // keepSingle: true,
      builder: (context) {
        return Container(
          width: ScreenUtil().screenWidth - 130.w,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.w)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextWidget.moreLine(
                text: title,
                fontSize: 16,
                color: Colors.black,
              ).padding(all: 26.w),
              CommonDivider(color: '#CCCCCC'.toColor, height: 0.5.w),
              SeDialogBtn(
                title: confirmTitle ?? 'Confirm',
                textColor: Colors.black,
                onTap: () async {
                  await SmartDialog.dismiss();
                  onConfirm?.call();
                },
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
      keepSingle: keepSingle,
    );
  }

  static void login({
    String? title,
    String description = '',
    String? confirmTitle,
    VoidCallback? onConfirm,
    String? tag,
    VoidCallback? onDismiss,
    bool keepSingle = false,
  }) {
    SmartDialog.show(
      // keepSingle: true,
      builder: (context) {
        String titleStr = title ?? 'Tips';
        return Container(
          width: ScreenUtil().screenWidth - 30.w,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
          padding: EdgeInsets.fromLTRB(20.w, 15.w, 20.w, 25.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (titleStr.notEmpty) ...[
                TextWidget.moreLine(text: titleStr, fontSize: 18, color: Colors.black),
                SizedBox(height: 20.w),
              ],
              if (description.notEmpty) ...[
                TextWidget.moreLine(text: description, fontSize: 15, color: Colors.black),
                SizedBox(height: 25.w),
              ],
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 7.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: ButtonWidget.text(
                        confirmTitle ?? 'Confirm',
                        textColor: Colors.white,
                        borderColor: Colors.transparent,
                        backgroundColor: SeAppColors.primary,
                        borderWidth: 0.5,
                        borderRadius: 45.w,
                        height: 45.w,
                        alignment: Alignment.center,
                        onTap: () async {
                          await SmartDialog.dismiss();
                          onConfirm?.call();
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
      keepSingle: keepSingle,
    );
  }

  static void twoButton({
    String? title,
    String description = '',
    String? cancelTitle,
    String? confirmTitle,
    Color? cancelTitleColor,
    Color? confirmTitleColor,
    VoidCallback? onCancel,
    VoidCallback? onConfirm,
    Widget? descriptionWidget,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        List<Widget> children = [];

        String titleStr = title ?? 'Tips';

        /// 标题
        if (titleStr.notEmpty) {
          children.addAll([
            TextWidget.moreLine(text: titleStr, fontSize: 18, color: Colors.black)
                .padding(horizontal: 30.w, vertical: 12.w),
          ]);
        }

        /// 描述
        if (descriptionWidget != null) {
          children.addAll([
            descriptionWidget,
            SizedBox(height: 25.w),
          ]);
        } else {
          if (description.notEmpty) {
            children.addAll([
              TextWidget.moreLine(text: description, fontSize: 14, color: Colors.black),
              SizedBox(height: 25.w),
            ]);
          }
        }

        return Container(
          width: ScreenUtil().screenWidth - 48.w * 2,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(12.w)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (children.isNotEmpty) ...children,
              CommonDivider(color: '#CCCCCC'.toColor, height: 0.5.w),
              Row(
                children: [
                  SeDialogBtn(
                    title: cancelTitle ?? 'Cancel',
                    textColor: cancelTitleColor ?? '#9A9A9A'.toColor,
                    onTap: () async {
                      await SmartDialog.dismiss();
                      onCancel?.call();
                    },
                  ).expanded(),
                  Container(
                    width: 0.5.w,
                    height: 42.w,
                    color: '#CCCCCC'.toColor,
                  ),
                  SeDialogBtn(
                    title: confirmTitle ?? 'Confirm',
                    textColor: confirmTitleColor ?? Colors.black,
                    onTap: () async {
                      await SmartDialog.dismiss();
                      onConfirm?.call();
                    },
                  ).expanded(),
                ],
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }
}

class SeDialogBtn extends StatelessWidget {
  const SeDialogBtn({required this.title, required this.textColor, required this.onTap, super.key});

  final String title;
  final VoidCallback onTap;
  final Color textColor;

  @override
  Widget build(BuildContext context) {
    return ButtonWidget.text(
      title,
      textColor: textColor,
      height: 42.w,
      fontSize: 16,
      alignment: Alignment.center,
      onTap: onTap,
      hasInkWell: false,
    );
  }
}
